//package org.linlinjava.litemall.wx.web;
//
//import com.github.pagehelper.PageInfo;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//import org.linlinjava.litemall.db.domain.LitemallBrandGrand;
//import org.linlinjava.litemall.db.domain.LitemallCommodity;
//import org.linlinjava.litemall.db.domain.LitemallCommodityComment;
//import org.linlinjava.litemall.db.service.LitemallBrandGrandService;
//import org.linlinjava.litemall.db.service.LitemallBrandService;
//import org.linlinjava.litemall.db.service.LitemallCommodityCommentService;
//import org.linlinjava.litemall.db.service.LitemallCommodityService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.event.ContextRefreshedEvent;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import java.math.BigDecimal;
//import java.time.LocalDateTime;
//import java.util.*;
//
/*表已经更改.如果要用的话..最后的一个方法有参考价值..allBrandGrand
* 思路
 //1.拿到所有商户的ID  从商户表中 //2拿到所有的商户分数表 的brand_id  set2
//行业均分 brand_id 为 1
//从订单表中找到 商品,从商品中
* */


///**
// * 定时任务:计算行业评论review平均值 .//每月1日执行一次  1日  执行影响  效率
// */
//
//
//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
//public class CommodityCommentAverageScore implements ApplicationListener<ContextRefreshedEvent> {
//    private final Log logger = LogFactory.getLog(CommodityCommentAverageScore.class);
//    //商品评论
//    @Autowired
//    private LitemallCommodityCommentService commodityCommentService;
//    @Autowired
//    private LitemallBrandGrandService brandGrandService;
//    @Autowired
//    private LitemallCommodityService commodityService;
//    @Autowired
//    private LitemallBrandService brandService;
//
//    //任务2:添加定时任务//每天凌晨2 点
//    @Scheduled(cron = "0 0 2 * * ?")//
//    private   void halfYearAllEveryMouth(){
//        avgAllcommodityCommentReview();
//        commentStar();
//    }
//
//    //任务1:启动时执行一次
//    @Override
//    public void onApplicationEvent(ContextRefreshedEvent event) {
//        /*if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次}*/
//        avgAllcommodityCommentReview();
//        commentStar();
//    }
//
//    //所有  商品评论(commodityComment)的评价(Review)..的平均分(avg)
//    private   void avgAllcommodityCommentReview(){
//        System.err.println("执行行业平均分:任务时间: " + LocalDateTime.now());
//        List<LitemallCommodityComment> commodityComments = commodityCommentService.byCommodityId();
//        //如果商品评论表有数据,执行
//        if( commodityComments.size() > 0 ){
//            BigDecimal avgAll = new BigDecimal("0");
//           // Map<String,String> map=new HashMap<>();
//            for (LitemallCommodityComment commodityComment : commodityComments) {
//                BigDecimal review = commodityComment.getReview();
//                avgAll= avgAll.add(review); }
//            BigDecimal divide = avgAll.divide(new BigDecimal(commodityComments.size()), 1, BigDecimal.ROUND_HALF_UP);
//            //商品评论是5分制度.显示10分所以乘 2
//            BigDecimal multiply = divide.multiply(new BigDecimal(2));
//            LitemallBrandGrand brandGrand = new LitemallBrandGrand();
////            brandGrandService.
//            //7位商户ID.. 但设置1为行业平均值
//            brandGrandService.delBrandGrandByBrandId(1);
//            brandGrand.setBrandGrand(String.valueOf(multiply));
//            brandGrand.setStarLevel("avgAllGrand");
//            brandGrand.setBrandId(1);
//            brandGrandService.addBrandGrand(brandGrand);
//            logger.info("行业平均分>>"  + divide );
//        }
//    }
//
//    //评分表已经 更改..方法作废..而且太影响效率 ...
//    //1.企业评分  共多少人评分, 5-1 分之间数量  ..和百分比...根据商户的产品.订单量显示
//    //商品评论的数据 commodityCommentService
//    private void commentStar() {
//        //1.拿到所有商户的ID
//        List<Integer> brandIds = brandService.brandIdList();
//        for (Integer brandId : brandIds) {
//            //商户ID....得到商品ID
//            Map<String,String> searchMap= new HashMap<>();
//            searchMap.put("brandId",brandId.toString());
//            Integer page =1;
//            Integer limit =100;
//            PageInfo<LitemallCommodity> commodityList = commodityService.findCommodityList(searchMap, page, limit);
//            Set<Integer> commodityIdSet=new HashSet<>();
//            for (LitemallCommodity litemallCommodity : commodityList.getList()) {
//                commodityIdSet.add(litemallCommodity.getId());
//            }
//            brandGrandService.delBrandGrandByBrandId(brandId);
//            //商品ID.去commodityComment.商品评论中找
//            Integer oneStar = 0 ;
//            Integer twoStar = 0 ;
//            Integer threeStar = 0 ;
//            Integer fourStar = 0 ;
//            Integer fiveStar = 0 ;
//            Integer totleStar = 0 ;
////            logger.info("commodityIdSet >>>" + commodityIdSet);
//            for (Integer commodityId : commodityIdSet) {
//                List<LitemallCommodityComment> litemallCommodityComments = commodityCommentService.byCommodityId(commodityId);
//                for (LitemallCommodityComment comment : litemallCommodityComments) {
//                    BigDecimal review = comment.getReview();
//                    if(review.equals(new BigDecimal("1"))){ oneStar++;}
//                    if(review.equals(new BigDecimal("2"))){twoStar++;}
//                    if(review.equals(new BigDecimal("3"))){threeStar++;}
//                    if(review.equals(new BigDecimal("4"))){fourStar++;}
//                    if(review.equals(new BigDecimal("5"))){fiveStar++;}
//                    totleStar=oneStar + twoStar +  threeStar +  fourStar + fiveStar;
//                }
//            }
//            Map<String,Object> map=new HashMap<>();
//            BigDecimal totleStarBigDecimal=new BigDecimal(totleStar);
//            BigDecimal oneBD = new BigDecimal(oneStar);
//            BigDecimal twoBD = new BigDecimal(twoStar);
//            BigDecimal threeBD = new BigDecimal(threeStar);
//            BigDecimal fourBD = new BigDecimal(fourStar);
//            BigDecimal fiveBD = new BigDecimal(fiveStar);
//
//            //如果总数大于0..才能求得 %
//            if(totleStarBigDecimal.compareTo(BigDecimal.ZERO)>0){
//                BigDecimal oneStarPercentage = oneBD.divide(totleStarBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
//                BigDecimal twoStarPercentage = twoBD.divide(totleStarBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
//                BigDecimal threeStarPercentage = threeBD.divide(totleStarBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
//                BigDecimal fourStarPercentage = fourBD.divide(totleStarBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
//                BigDecimal fiveStarPercentage = fiveBD.divide(totleStarBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
//                //均值 共计星数 /单数
//                BigDecimal AVGPercentage= (
//                        oneBD.multiply(new BigDecimal("1"))
//                                .add(twoBD.multiply(new BigDecimal("2")))
//                                .add(threeBD.multiply(new BigDecimal("3")))
//                                .add(fourBD.multiply(new BigDecimal("4")))
//                                .add(fiveBD.multiply(new BigDecimal("5")))
//                )
//                        .divide(totleStarBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
//
//                LitemallBrandGrand totleBrandGrand = new LitemallBrandGrand();
//                totleBrandGrand.setBrandId(brandId);
//                totleBrandGrand.setStarLevel("total");
//                totleBrandGrand.setBrandGrand(String.valueOf(totleStar));
//                brandGrandService.addBrandGrand(totleBrandGrand);
//
//                LitemallBrandGrand AVGStarBrandGrand = new LitemallBrandGrand();
//                AVGStarBrandGrand.setBrandId(brandId);
//                AVGStarBrandGrand.setStarLevel("AVGPercentage");
//                AVGStarBrandGrand.setBrandGrand(AVGPercentage.toString().substring(0,3));
//                brandGrandService.addBrandGrand(AVGStarBrandGrand);
//
//                LitemallBrandGrand oneStarBrandGrand = new LitemallBrandGrand();
//                oneStarBrandGrand.setBrandId(brandId);
//                oneStarBrandGrand.setStarLevel("oneStarPercentage");
//                oneStarBrandGrand.setBrandGrand(oneStarPercentage.multiply(new BigDecimal("100")).toString().substring(0,5) + "%" );
//                brandGrandService.addBrandGrand(oneStarBrandGrand);
//
//                LitemallBrandGrand twoStarBrandGrand = new LitemallBrandGrand();
//                twoStarBrandGrand.setBrandId(brandId);
//                twoStarBrandGrand.setStarLevel("twoStarPercentage");
//                twoStarBrandGrand.setBrandGrand(twoStarPercentage.multiply(new BigDecimal("100")).toString().substring(0,5) + "%");
//                brandGrandService.addBrandGrand(twoStarBrandGrand);
//
//                LitemallBrandGrand threeStarBrandGrand = new LitemallBrandGrand();
//                threeStarBrandGrand.setBrandId(brandId);
//                threeStarBrandGrand.setStarLevel("threeStarPercentage");
//                threeStarBrandGrand.setBrandGrand(threeStarPercentage.multiply(new BigDecimal("100")) .toString().substring(0,5)+ "%");
//                brandGrandService.addBrandGrand(threeStarBrandGrand);
//
//                LitemallBrandGrand fourStarBrandGrand = new LitemallBrandGrand();
//                fourStarBrandGrand.setBrandId(brandId);
//                fourStarBrandGrand.setStarLevel("fourStarPercentage");
//                fourStarBrandGrand.setBrandGrand(fourStarPercentage.multiply(new BigDecimal("100")).toString().substring(0,5) + "%");
//                brandGrandService.addBrandGrand(fourStarBrandGrand);
//
//                LitemallBrandGrand fiveStarBrandGrand = new LitemallBrandGrand();
//                fiveStarBrandGrand.setBrandId(brandId);
//                fiveStarBrandGrand.setStarLevel("fiveStarPercentage");
//                fiveStarBrandGrand.setBrandGrand(fiveStarPercentage.multiply(new BigDecimal("100")).toString().substring(0,5) + "%");
//                brandGrandService.addBrandGrand(fiveStarBrandGrand);
//            }
//        }
//    }
//

//}
//public  Object allBrandGrand(){
//        //1.拿到所有商户的ID  从商户表中
//        List<Integer> brandIds = brandService.brandIdList();
//        Set<Integer> brandIdListSet = new HashSet<>(brandIds);
//
//        //2拿到所有的商户分数表 的brand_id  set2
//        Set<Integer> brandIdSet = brandGrandService.brandIdList();
//        //1 减去2  得到 哪些商户ID没有 写入 得分表中
//        brandIdListSet.removeAll(brandIdSet);
//        brandIds.clear();
//        brandIds.addAll(brandIdListSet);
//        //行业均分 brand_id 为 1
//        LitemallBrandGrand brandGrandAVG = new LitemallBrandGrand();
//        brandGrandAVG.setBrandId(1);
//        brandGrandService.addBrandGrand(brandGrandAVG);
//        //其他企业ID进入循环
//        for (Integer brandId : brandIds) {
//        LitemallBrandGrand brandGrand = new LitemallBrandGrand();
//        brandGrand.setBrandId(brandId);
//        brandGrandService.addBrandGrand(brandGrand);
//        }
//        //从订单表中找到 商品,从商品中
//        //商品评论的数据 commodityCommentService
//        // commodityCommentService.
//        return ResponseUtil.ok();
//        }
